package cn.com.dycc.sys_manage.deptsysmanagement.service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import cn.com.dycc.common.log.Constants;
import cn.com.dycc.common.log.SystemServiceLog;
import cn.com.dycc.sys_manage.deptsysmanagement.dao.DeptDao;
import cn.com.dycc.sys_manage.deptsysmanagement.entity.Dept;
import cn.com.dycc.sys_manage.deptsysmanagement.entity.DeptResult;
import cn.com.dycc.sys_manage.dictionarysysmanagement.dao.DictionaryDao;
import cn.com.dycc.sys_manage.rolesysmanagement.service.RoleNotFoundException;
import cn.com.dycc.sys_manage.usersysmanagement.dao.UserChildAccountDao;
import cn.com.dycc.sys_manage.usersysmanagement.dao.UserDao;
import cn.com.dycc.util.DateUtil;
import cn.com.dycc.util.DeptTree;
import cn.com.dycc.util.IntegerUtil;
import cn.com.dycc.util.LayDeptTreeUtil;
import cn.com.dycc.util.NewDeptTreeUtil;

/**
 * 单位/部门管理业务层
 * Description: 
 * Author: linfangan
 * Version: 1.0
 * Create Date Time: 2017年8月15日 上午11:31:34.
 * Update Date Time: 
 * @see
 */
@Service("deptService")
@Transactional(rollbackFor=Exception.class)
public class DeptServiceImpl implements DeptService{
	
	@Resource(name="deptDao")
	private DeptDao deptDao;
	
//	@Resource(name="roleUserDao")
//	private RoleUserDao roleUserDao;

	@Resource(name="userChildAccountDao")
	private UserChildAccountDao  userChildAccountDao;
	
	@Resource(name="dictionaryDao")
	private DictionaryDao dictionaryDao;
	
	@Resource(name="userDao")
	private UserDao userDao;
	
//	//增加单位/部门信息
//	public Dept addDept(Dept dept,List<Integer> bindRoleNo) throws DeptNotFoundException,RoleNotFoundException {
//		//判断是否为系统管理员或者单位管理员
//		boolean guess = true;
//		for(int i=0;i<bindRoleNo.size();i++){
//			Integer bind = bindRoleNo.get(i);
//			if(bind==9||bind==10){
//				guess = false;
//				break;
//			}
//		}
//		if(guess){
//			throw new RoleNotFoundException("组织管理需要权限为系统管理员或单位管理员！");
//		}
//		String deptName = dept.getDept_name();
//		if(deptName==null||deptName.trim().isEmpty()){
//			throw new DeptNotFoundException("单位名称不能为空！");
//		}
//		//取全部单位名称对比
//		List<String> allDeptName = deptDao.findAllDeptName();
//		for(int i = 0 ;i<allDeptName.size();i++){
//			if(deptName.equals(allDeptName.get(i))){
//				throw new DeptNotFoundException("单位名称已被创建！");
//			}
//		}
//		String deptAddr = dept.getDept_addr();
//		String deptInfo = dept.getDept_info();
//		Integer deptPid = dept.getPid();
//		Integer areaId = dept.getArea_id();
//		Integer deptAreaId = areaDao.findAreaIdByAreaCode(areaId);
//		String deptPer = dept.getDept_per();
//		String deptTel = dept.getDept_tel();
//		String deptRemark = dept.getRemark();
//		
//		Dept addDept = new Dept();
//		Integer deptOrderNo = dept.getOrder_no();
//		if(deptOrderNo==null){
//			addDept.setOrder_no(null);
//		}
//		addDept.setDept_name(deptName);
//		addDept.setDept_info(deptInfo);
//		addDept.setPid(deptPid);
//		addDept.setHidden("N");
//		addDept.setArea_id(deptAreaId);
//		addDept.setDept_addr(deptAddr);
//		addDept.setDept_per(deptPer);
//		addDept.setDept_tel(deptTel);
//		addDept.setOrder_no(deptOrderNo);
//		addDept.setRemark(deptRemark);
//		
//		int add = deptDao.addDept(addDept);
//		if(add>0){
//			return addDept;
//		}else{
//			return null;
//		}
//	}
	
	//查找所有单位/部门信息 树状图显示
	public List<Object> findAllDept(String org_id,String send_dept_id) throws DeptNotFoundException,RoleNotFoundException {
		
		//查询pid为0的单位/部门
		String dept_no = deptDao.selDeptNoByPidOfAll(org_id);
		if(dept_no==null){
			throw new DeptNotFoundException("加载失败!");
		}
		//接收pid为0的单位/部门下面子单位
		List<Dept> listDept = new ArrayList<Dept>();
		try {
			listDept = deptDao.findAllDeptByShowChildLst(dept_no,org_id);
		} catch (Exception e) {
			e.printStackTrace();
			throw new DeptNotFoundException("查询失败!");
		}
		//获取顶级单位的pid
		Dept d = deptDao.findDeptByDeptNo(dept_no,org_id);
		String topPid = d.getPid();
//		System.out.println(listDept);
		DeptTree deptList = new DeptTree();
		return deptList.deptList(listDept,topPid,send_dept_id);
	}
	
	//插入或更新单位/部门信息
	public String updateDept(Dept dept) throws DeptNotFoundException,RoleNotFoundException {
//		System.out.println("dept="+dept);
		String deptId = dept.getDept_no();
		Dept aldept = deptDao.findDeptByDeptNo(deptId,dept.getOrg_id());
		if(aldept!=null){
			String deptName = dept.getDept_name();
			if(deptName==null||deptName.trim().isEmpty()){
				throw new DeptNotFoundException("单位名称不能为空！");
			}
//			//取全部单位名称对比
//			List<String> allDeptName = deptDao.findAllDeptName();
//			String dept_name  = aldept.getDept_name();
//			if(!dept_name.equals(deptName)){
//				for(int i = 0 ;i<allDeptName.size();i++){
//					if(deptName.equals(allDeptName.get(i))){
//						throw new DeptNotFoundException("单位名称已被创建！");
//					}
//				}
//			}
			String deptAddr = dept.getDept_addr();
			String deptInfo = dept.getDept_info();
			//新pid
			String deptPid = dept.getPid();
			//原部门pid
			String old_top_dept_no = deptDao.findPidBydeptNo(deptId,dept.getOrg_id());
			if(!(deptPid.equals(old_top_dept_no))){
				if(("0".equals(deptId))){
					throw new DeptNotFoundException("不能将非顶级部门更新为顶级单位！");
				}
				//父查子部门id，含父id
				try {
					List<String> list_user_id = new ArrayList<String>();
					list_user_id = userChildAccountDao.findUserIdByDeptNo(deptId);
					for(String user_id : list_user_id){
						Map<String,Object> map = new HashMap<String,Object>();
						map.put("user_id",user_id);
						map.put("top_dept_no",deptPid);
						userChildAccountDao.updateTopDeptNoByUserId(map);
					}
				} catch (Exception e) {
					e.printStackTrace();
					throw new RoleNotFoundException("转移的部门人员更新失败!");
				}
				
			}
			String areaId = dept.getArea_id();
			String deptPer = dept.getDept_per();
			String deptTel = dept.getDept_tel();
			String deptRemark = dept.getRemark();
			String dept_flag = dept.getDept_flag();
			String org_id = dept.getOrg_id();
			String update_user_id = dept.getUpdate_user_id();
			String state = dept.getState();
			if(!aldept.getDept_name().equals(deptName)){
				//判断同级部门名称是否已存在
				int count = deptDao.dicideName(org_id, deptPid,deptName);
				if(count>=1){
					throw new RoleNotFoundException("该部门名称已存在!");
				}
			}
						
			if("N".equals(state)){
				List<Dept> listDept = deptDao.findDeptByparentId(deptId,org_id);
				if(!listDept.isEmpty()){
					throw new RoleNotFoundException("该部门下还有子部门！");
				}
				List<String> listUserId = 
						userChildAccountDao.findUserIdByDeptNo(deptId);
				if(!(listUserId.isEmpty())){
					throw new RoleNotFoundException("该部门下还有人员！");
				}
			}
			
			Dept updateDept = new Dept();
			Integer deptOrderNo = dept.getOrder_no();
			if(deptOrderNo==null){
				updateDept.setOrder_no(null);
			}
			updateDept.setDept_no(deptId);
			updateDept.setDept_name(deptName);
			updateDept.setDept_info(deptInfo);
			updateDept.setPid(deptPid);
			updateDept.setState(state);
			updateDept.setArea_id(areaId);
			updateDept.setDept_addr(deptAddr);
			updateDept.setDept_per(deptPer);
			updateDept.setDept_tel(deptTel);
			updateDept.setOrder_no(deptOrderNo);
			updateDept.setRemark(deptRemark);
			updateDept.setDept_flag(dept_flag);
			updateDept.setOrg_id(org_id);
			updateDept.setUpdate_user_id(update_user_id);
			String format = "yyyy-MM-dd HH:mm:ss";
			String update_date = DateUtil.getNowDate(format);
			updateDept.setUpdate_date(update_date);
			
			int up = deptDao.updateDept(updateDept);
			if(up<=0){
				throw new DeptNotFoundException("更新失败！");
			}else{
				return "更新成功！";
			}
			
		}else{
			String deptName = dept.getDept_name();
			if(deptName==null||deptName.trim().isEmpty()){
				throw new DeptNotFoundException("单位名称不能为空！");
			}
//			//取全部单位名称对比
//			List<String> allDeptName = deptDao.findAllDeptName();
//			for(int i = 0 ;i<allDeptName.size();i++){
//				if(deptName.equals(allDeptName.get(i))){
//					throw new DeptNotFoundException("单位名称已被创建！");
//				}
//			}
			String deptAddr = dept.getDept_addr();
			String deptInfo = dept.getDept_info();
			String deptPid = dept.getPid();
			if("0".equals(deptPid)){
				throw new DeptNotFoundException("不能创建pid为0的最顶级单位！");
			}
			String areaId = dept.getArea_id();
			String deptPer = dept.getDept_per();
			String deptTel = dept.getDept_tel();
			String deptRemark = dept.getRemark();
			String dept_flag = dept.getDept_flag();
			String org_id = dept.getOrg_id();
			String update_user_id = dept.getUpdate_user_id();
			
			//判断同级部门名称是否已存在
			int count = deptDao.dicideName(org_id, deptPid,deptName);
			if(count>=1){
				throw new RoleNotFoundException("该部门名称已存在!");
			}
			
			Dept addDept = new Dept();
			Integer deptOrderNo = dept.getOrder_no();
			if(deptOrderNo==null){
				addDept.setOrder_no(null);
			}
			addDept.setDept_name(deptName);
			addDept.setDept_info(deptInfo);
			addDept.setPid(deptPid);
			addDept.setArea_id(areaId);
			addDept.setDept_addr(deptAddr);
			addDept.setDept_per(deptPer);
			addDept.setDept_tel(deptTel);
			addDept.setOrder_no(deptOrderNo);
			addDept.setRemark(deptRemark);
			addDept.setDept_flag(dept_flag);
			addDept.setOrg_id(org_id);
			addDept.setOpt_user_id(update_user_id);
			
			int add = deptDao.addDept(addDept);
			if(add<=0){
				throw new DeptNotFoundException("增加失败！");
			}else{
				return "增加成功！";
			}
		}

	}
	
	//根据id查找单位/部门信息
	public DeptResult findAllInfoByDeptNo(HttpServletRequest request,String dept_no) throws DeptNotFoundException,RoleNotFoundException {
		if(dept_no==null){
			throw new DeptNotFoundException("请选择需要查找的单位/部门");
		}
		String org_id = request.getSession().getAttribute("org_id").toString();
		DeptResult dept = deptDao.findAllInfoByDeptNo(dept_no,org_id);
		
		if(dept==null){
			throw new DeptNotFoundException("查询失败!");
		}
		
		return dept;
	}
	
//	//根据数组id删除单位/部门信息
//	public String deleteDeptsByIds(String... ids) throws DeptNotFoundException,RoleNotFoundException {
//		if(ids.length==0){
//			throw new RoleNotFoundException("请选择需要删除的单位/部门");
//		}
//		int del = deptDao.deleteDeptsByIds(ids);
//		if(del!=ids.length){
//			throw new RoleNotFoundException("删除失败！");
//		}
//		return "删除成功！";
//	}
	

	
	
	//根据id删除部门/单位
	@SystemServiceLog(description=Constants.T_DEPT_DELETE)
	public String deleteDeptByDeptNo(HttpServletRequest request,String dept_no)
			throws DeptNotFoundException, RoleNotFoundException {
		if(dept_no==null){
			throw new RoleNotFoundException("请选择删除的部门/单位");
		}
		
		String org_id = request.getSession().getAttribute("org_id").toString();
		List<Dept> listDept = deptDao.findDeptByparentId(dept_no,org_id);
		if(!listDept.isEmpty()){
			throw new RoleNotFoundException("该部门下还有子部门！");
		}
		List<String> listUserId = userChildAccountDao.findUserIdByDeptNo(dept_no);
		if(!listUserId.isEmpty()){
			throw new RoleNotFoundException("该部门下还有人员！");
		}
		int delDept = deptDao.deleteDeptByDeptNo(dept_no,org_id);
		if(delDept<=0){
			throw new RoleNotFoundException("删除失败！");
		}
		return "删除成功！";
	}
	
//	//查询id最大值
//	public int findMaxId() {
//		int maxId = deptDao.findMaxId();
//		return maxId;
//	}
//	
//	//获取id
//	public int getDeptNo() {
//		int deptNo = deptDao.getDeptNo();
//		return deptNo;
//	}
	
	//显示选中的部门单位树
	public List<Object> findAloneDept(HttpServletRequest request,String bindTop_dept_no,String send_dept_no) throws DeptNotFoundException, RoleNotFoundException {
		
		String org_id = request.getSession().getAttribute("org_id").toString();
		if(bindTop_dept_no==null){
			throw new DeptNotFoundException("请先选择单位！");
		}
		List<Dept> list = new ArrayList<Dept>();
		try {
			list = deptDao.findAllDeptByShowChildLst(bindTop_dept_no,org_id);
		} catch (Exception e) {
			e.printStackTrace();
			throw new DeptNotFoundException("查找失败！请稍后再试");
		}
		Dept d = deptDao.findDeptByDeptNo(bindTop_dept_no,org_id);
		String topPid = d.getPid();
		DeptTree deptList = new DeptTree();
		return deptList.deptList(list, topPid,send_dept_no);
		
	}
	
	//用户普通用户的单位树展示，全部门
	public List<Object> findAllDeptOfCommonUser(String org_id, String send_dept_id)
			throws DeptNotFoundException, RoleNotFoundException {
		
		//查询pid为0的单位/部门
		String dept_no = deptDao.selDeptNoByPidOfAll(org_id);
		if(dept_no==null){
			throw new DeptNotFoundException("加载失败!");
		}
		//接收pid为0的单位/部门下面子单位
		List<Dept> listDept = new ArrayList<Dept>();
		try {
			listDept = deptDao.callShowDeptOfStateChildLst(dept_no,org_id);
		} catch (Exception e) {
			e.printStackTrace();
			throw new DeptNotFoundException("查询失败!");
		}
	//	System.out.println(listDept);
		//获取顶级单位的pid
		Dept d = deptDao.findDeptByDeptNo(dept_no,org_id);
		String topPid = d.getPid();
//		System.out.println(listDept);
		DeptTree deptList = new DeptTree();
		return deptList.deptList(listDept,topPid,send_dept_id);
		
	}
	
	//显示选中的部门单位树 普通用户展示
	public List<Object> findAloneCommon(HttpServletRequest request,String bindTop_dept_no, String send_dept_no)
			throws DeptNotFoundException, RoleNotFoundException {
		
		String org_id = request.getSession().getAttribute("org_id").toString();
		
		if(bindTop_dept_no==null){
			throw new DeptNotFoundException("请先选择单位！");
		}
		List<Dept> list = new ArrayList<Dept>();
		try {
			list = deptDao.callShowDeptOfStateChildLst(bindTop_dept_no,org_id);
		} catch (Exception e) {
			throw new DeptNotFoundException("查找失败！稍后再试");
		}
		Dept d = deptDao.findDeptByDeptNo(bindTop_dept_no,org_id);
		String topPid = d.getPid();
		DeptTree deptList = new DeptTree();
		return deptList.deptList(list, topPid,send_dept_no);
	}
	
	//20180503新建管理员显示部门  HttpServletRequest request,
	public List<Object> findAllDeptNew(HttpServletRequest request,String send_dept_id) throws DeptNotFoundException, RoleNotFoundException {
		
		String org_id = request.getSession().getAttribute("org_id").toString();
//		String org_id = "1";
		
		String dept_no = request.getSession().getAttribute("dept_no").toString();
		if(dept_no==null){
			throw new DeptNotFoundException("请登录！");
		}
		
		List<Dept> listDept = new ArrayList<Dept>();
		//管理员展示全状态查询
		String state = null;
		
		try {
			listDept = deptDao.findAllDeptNew(org_id,state);
		} catch (Exception e) {
			e.printStackTrace();
			throw new DeptNotFoundException("查找失败！稍后再试");
		}
		NewDeptTreeUtil deptList = new NewDeptTreeUtil();
		return deptList.deptListofLogin(listDept,send_dept_id,dept_no);
	}
	
	
	//20180503新建部门更新和部门插入  插入接口
	@SystemServiceLog(description=Constants.T_DEPT_ADD)
	public String addAndUpdate(HttpServletRequest request,Dept dept) throws DeptNotFoundException {
		
//		System.out.println("dept="+dept);
		String org_id = request.getSession().getAttribute("org_id").toString();
		String update_user_id = request.getSession().getAttribute("id").toString();

		String deptName = dept.getDept_name();
		if(deptName==null||deptName.trim().isEmpty()){
			throw new DeptNotFoundException("部门名称不能为空！");
		}

		String deptAddr = dept.getDept_addr();
		String deptInfo = dept.getDept_info();
		String deptPid = dept.getPid();
		if(deptPid==null||deptPid.trim().isEmpty()){
			throw new DeptNotFoundException("上级不能pid为空！");
		}
		String areaId = dept.getArea_id();
		String deptPer = dept.getDept_per();
		String deptTel = dept.getDept_tel();
		if(deptTel!=null){
			boolean tel_num = IntegerUtil.isNumeric(deptTel);
			if(!tel_num){
				throw new RoleNotFoundException("电话号码请输入数字!");
			}
		}
		String deptRemark = dept.getRemark();
		String dept_flag = dept.getDept_flag();
//		String org_id = dept.getOrg_id();
//		String update_user_id = dept.getUpdate_user_id();
			
		//判断同级部门名称是否已存在
		int count = deptDao.dicideName(org_id, deptPid,deptName);
		if(count>=1){
			throw new RoleNotFoundException("该部门名称已存在!");
		}
			
		Dept addDept = new Dept();
		Integer deptOrderNo = dept.getOrder_no();
//		System.out.println("deptOrderNo="+deptOrderNo);
		if(deptOrderNo!=null){
			boolean order_num = IntegerUtil.isNumeric(deptOrderNo.toString());
			if(!order_num){
				throw new RoleNotFoundException("排序请输入数字！");
			}
		}
		addDept.setDept_name(deptName);
		addDept.setDept_info(deptInfo);
		addDept.setPid(deptPid);
		addDept.setArea_id(areaId);
		addDept.setDept_addr(deptAddr);
		addDept.setDept_per(deptPer);
		addDept.setDept_tel(deptTel);
		addDept.setOrder_no(deptOrderNo);
		addDept.setRemark(deptRemark);
		addDept.setDept_flag(dept_flag);
		addDept.setOrg_id(org_id);
		addDept.setOpt_user_id(update_user_id);
			
		int add = deptDao.addDept(addDept);
		if(add<=0){
			throw new DeptNotFoundException("增加失败！");
		}else{
			return "增加成功！";
		}
	}
	
	//20180503新建普通用户显示部门
	public List<Object> findAllDeptOfCommonUserNew(HttpServletRequest request,String send_dept_id)
			throws DeptNotFoundException, RoleNotFoundException {
		
		String org_id = request.getSession().getAttribute("org_id").toString();
//		String org_id = "1";
		
		List<Dept> listDept = new ArrayList<Dept>();
		//管理员展示全状态查询
		String state = "Y";
		
		try {
			listDept = deptDao.findAllDeptNew(org_id,state);	
		} catch (Exception e) {
			e.printStackTrace();
			throw new DeptNotFoundException("查找失败！稍后再试");
		}
		NewDeptTreeUtil deptList = new NewDeptTreeUtil();
		return deptList.deptList(listDept,send_dept_id);
	}
	
	//20180503更新部门接口
	@SystemServiceLog(description=Constants.T_DEPT_UPDATE)
	public String newUpdateDept(HttpServletRequest request, Dept dept) throws DeptNotFoundException {
		
		String org_id = request.getSession().getAttribute("org_id").toString();
		String update_user_id = request.getSession().getAttribute("id").toString();
		
//		System.out.println("dept="+dept);
		
		String deptId = dept.getDept_no();
//		System.out.println("deptId="+deptId);
		
		Dept aldept = deptDao.findDeptByDeptNo(deptId,org_id);
		
		if(aldept==null){
			throw new DeptNotFoundException("未查询到更新部门的信息！");
		}
		
		String deptName = dept.getDept_name();
		if(deptName==null||deptName.trim().isEmpty()){
			throw new DeptNotFoundException("单位名称不能为空！");
		}
		String deptAddr = dept.getDept_addr();
		String deptInfo = dept.getDept_info();
		//新pid
//		String deptPid = dept.getPid();
//		
//		if(deptPid==null||deptPid.trim().isEmpty()){
//			throw new DeptNotFoundException("上级pid不能为空！");
//		}
		//原部门pid
//		String old_top_dept_no = deptDao.findPidBydeptNo(deptId);
//		
//		if(!(deptPid.equals(old_top_dept_no))&&!("0".equals(deptPid))){
//			throw new DeptNotFoundException("不能更新为自身子部门！");
//		}
//		
//		if("0".equals(deptPid)){
//			//父查子部门id，含父id
//			try {
//				List<String> list_user_id = new ArrayList<String>();
//				list_user_id = userChildAccountDao.findUserIdByDeptNo(deptId);
//				for(String user_id : list_user_id){
//					Map<String,Object> map = new HashMap<String,Object>();
//					map.put("user_id",user_id);
//					map.put("top_dept_no",deptPid);
//					userChildAccountDao.updateTopDeptNoByUserId(map);
//				}
//			} catch (Exception e) {
//				e.printStackTrace();
//				throw new RoleNotFoundException("转移的部门人员更新失败!");
//			}
//			
//		}
		
		String areaId = dept.getArea_id();
		String deptPer = dept.getDept_per();
		String deptTel = dept.getDept_tel();
		if(deptTel!=null){
			boolean tel_num = IntegerUtil.isNumeric(deptTel);
			if(!tel_num){
				throw new RoleNotFoundException("电话号码请输入数字!");
			}
		}
		String deptRemark = dept.getRemark();
		String dept_flag = dept.getDept_flag();
//			String org_id = dept.getOrg_id();
//			String update_user_id = dept.getUpdate_user_id();
		String state = dept.getState();
//		if(!aldept.getDept_name().equals(deptName)||!(aldept.getPid().equals(deptPid))){
//			//判断同级部门名称是否已存在
//			int count = deptDao.dicideName(org_id, deptPid,deptName);
//			if(count>=1){
//				throw new RoleNotFoundException("该部门名称已存在!");
//			}
//		}
					
		if("N".equals(state)){
			List<Dept> listDept = deptDao.findDeptByparentId(deptId,org_id);
			if(!listDept.isEmpty()){
				throw new RoleNotFoundException("该部门下还有子部门！");
			}
			List<String> listUserId = 
					userChildAccountDao.findUserIdByDeptNo(deptId);
			if(!(listUserId.isEmpty())){
				throw new RoleNotFoundException("该部门下还有人员！");
			}
		}
		
		Dept updateDept = new Dept();
		Integer deptOrderNo = dept.getOrder_no();
		if(deptOrderNo!=null){
			boolean order_num = IntegerUtil.isNumeric(deptOrderNo.toString());
			if(!order_num){
				throw new RoleNotFoundException("排序请输入数字！");
			}
		}
		updateDept.setDept_no(deptId);
		updateDept.setDept_name(deptName);
		updateDept.setDept_info(deptInfo);
//		updateDept.setPid(deptPid);
		updateDept.setState(state);
		updateDept.setArea_id(areaId);
		updateDept.setDept_addr(deptAddr);
		updateDept.setDept_per(deptPer);
		updateDept.setDept_tel(deptTel);
		updateDept.setOrder_no(deptOrderNo);
		updateDept.setRemark(deptRemark);
		updateDept.setDept_flag(dept_flag);
		updateDept.setOrg_id(org_id);
		updateDept.setUpdate_user_id(update_user_id);
		String format = "yyyy-MM-dd HH:mm:ss";
		String update_date = DateUtil.getNowDate(format);
		updateDept.setUpdate_date(update_date);
		
		int up = deptDao.updateDept(updateDept);
		
		if(up<=0){
			throw new RoleNotFoundException("更新失败！");
		}
		return "更新成功！";
	}
	
	//layui部门树
	public List<Object> layDeptTree(HttpServletRequest request, String state) throws DeptNotFoundException {
		
		String org_id = request.getSession().getAttribute("org_id").toString();
		
		List<Dept> listDept = new ArrayList<Dept>();
		//管理员展示全状态查询
		List<Object> list = new ArrayList<Object>();
		
		try {
			listDept = deptDao.findAllDeptNew(org_id,state);
			LayDeptTreeUtil l = new LayDeptTreeUtil();
			list = l.PartyList(listDept);
		} catch (Exception e) {
			e.printStackTrace();
			throw new DeptNotFoundException("查找失败！稍后再试");
		}		
		return list;
	}
	
}
